<?php

namespace app\common\model;

use think\Model;

class UserQuestion extends Model
{
    // 表名
    protected $name = 'user_question';

    /**
     * 添加组卷
     * @param mixed $user_id 
     * @param mixed $question_group_id 
     * @return mixed 
     */
    public function saveUserQuestionAdd($user_id, $question_group_id)
    {
        $question_group = (new QuestionGroup())->get($question_group_id);
        if (!$question_group) {
            fail('刷题组不存在');
        }
        $row = $this->where([
            'user_id' => $user_id,
            'question_group_id' => $question_group_id,
        ])->find();
        if ($row) {
            fail('您已添加过此刷题组');
        }
        $data = [
            'user_id' => $user_id,
            'question_group_id' => $question_group_id,
            'question_cate_id' => $question_group->question_cate_pid,
            'question_cate_id2' => $question_group->question_cate_id,
            'createtime' => time()
        ];
        $this->save($data);
        return true;
    }

    /**
     * 移除刷题
     * @param mixed $user_id 
     * @param mixed $question_group_id 
     * @return mixed 
     */
    public function removeUserQuestion($user_id, $question_group_id)
    {
        $row = $this->where(['user_id' => $user_id, 'question_group_id' => $question_group_id])->find();
        if (!$row) {
            fail('您未添加此刷题');
        }
        return self::where(['user_id' => $user_id, 'question_group_id' => $question_group_id])->delete();
    }

    /**
     * 获取用户添加刷题组
     * @param mixed $user_id 
     * @param mixed $question_group_id 
     * @return mixed 
     */
    public function getUserQusetionAddIs($user_id)
    {
        return self::where(['user_id' => $user_id])->column('question_group_id');
    }
}
